package com.household.controller;

import com.household.domain.entity.User;
import com.household.domain.vo.AdminUserVO;
import com.household.domain.vo.UserInfoVO;
import com.household.service.OrdersService;
import com.household.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created By wanzhen
 * 2023. 2023/4/4. 18:47
 */

@Controller
@RequestMapping("/admin")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class AdminPageController {

    private final UserService userService;

    private final OrdersService ordersService;

    @Autowired
    public AdminPageController(UserService userService, OrdersService ordersService) {
        this.userService = userService;
        this.ordersService = ordersService;
    }

    /*
    * 仪表盘页面
    * */
    @RequestMapping("/dashboard")
    public String showDashboardPage(@AuthenticationPrincipal User user, ModelMap modelMap){
        String frontName = userService.getFrontName(user);
        modelMap.put("frontName",frontName);
        Map<String,Long> data=
                userService.getAdminDashboardData();
        String userDesc = "今日注册用数：" + data.get("today") +
                "，总用户数：" + data.get("total") +
                "，其中禁用用户数：" + data.get("disEnable") +
                "，冻结用户数：" + data.get("lock");
        modelMap.put("userDesc",userDesc);

        Map<String,Integer> data1=ordersService.getAdminDashboardData();
        String orderDesc = "今日新增订单数：" + data1.get("today") +
                "，总等待接单数：：" + data1.get("wait") +
                "，正在派送数：" + data1.get("transport");
        modelMap.put("orderDesc", orderDesc);

        return "dashboard";

    }


    /**
     * 订单管理页面
     */
    @RequestMapping("/order")
    public String showOrderPage(@AuthenticationPrincipal User sysUser, ModelMap map) {
        map.put("frontName", userService.getFrontName(sysUser));
        return "order";
    }

    /**
     * 订单回收页面
     */
    @RequestMapping("/recycle")
    public String showRecyclePage(@AuthenticationPrincipal User user,ModelMap modelMap){
        modelMap.put("frontName", userService.getFrontName(user));
        return "recycle";
    }

    /**
     * 用户管理页面
     */
    @RequestMapping("/user")
    public String showUser(@AuthenticationPrincipal User user, ModelMap map) {
        map.put("frontName", userService.getFrontName(user));
        return "user";
    }

    /**
     * 个人中心页面
     */
    @RequestMapping("/info")
    public String showInfoPage(@AuthenticationPrincipal User sysUser, ModelMap map) {
        map.put("frontName", userService.getFrontName(sysUser));
        UserInfoVO userInfo = userService.getUserInfo(sysUser.getId());
        map.put("info", userInfo);
        return "info";
    }

    /**
     * 反馈管理页面
     */
    @RequestMapping("/feedback")
    public String showFeedbackPage(@AuthenticationPrincipal User sysUser, ModelMap map) {
        map.put("frontName", userService.getFrontName(sysUser));
        return "feedback";
    }

}
